﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace RedPanda.MusicPlayer
{
    class MusicQuery
    {
        private XDocument lib;
        private string path;

        public MusicQuery(string uri)
        {
            lib = XDocument.Load(uri);
            path = uri;
        }

        public void groupByAbulm(string output)
        {
            XDocument xdoc = new XDocument(new XElement("library"));
            var albums = from album in lib.Descendants("album")
                         select album.Value;
            albums = albums.Distinct();
            foreach (var album in albums)
            {
                XElement xele = new XElement("album", new XElement("tracks"));
                xele.SetAttributeValue("title", album);
                var tracks = from track in lib.Descendants("track")
                             where track.Element("album").Value.Equals(album)
                             select track;
                foreach (var track in tracks)
                {
                    xele.Element("tracks").Add(track);
                }
                xdoc.Element("library").Add(xele);
            }
            xdoc.Save(output);
        }
        public void groupByArtist(string output)
        {
            XDocument xdoc = new XDocument(new XElement("library"));
            var artists = from artist in lib.Descendants("artist")
                          select artist.Value;
            artists = artists.Distinct();
            foreach (var artist in artists)
            {
                XElement xele = new XElement("artist", new XElement("tracks"));
                xele.SetAttributeValue("name", artist);
                var tracks = from track in lib.Descendants("track")
                             where track.Element("artist").Value.Equals(artist)
                             select track;
                foreach (var track in tracks)
                {
                    xele.Element("tracks").Add(track);
                }
                xdoc.Element("library").Add(xele);
            }
            xdoc.Save(output);
        }
        public void groupByGenre(string output)
        {
            XDocument xdoc = new XDocument(new XElement("library"));
            var genres = from genre in lib.Descendants("genre")
                         select genre.Value;
            genres = genres.Distinct();
            foreach (var genre in genres)
            {
                XElement xele = new XElement("genre", new XElement("tracks"));
                xele.SetAttributeValue("name", genre);
                var tracks = from track in lib.Descendants("track")
                             where track.Element("genre").Value.Equals(genre)
                             select track;
                foreach (var track in tracks)
                {
                    xele.Element("tracks").Add(track);
                }
                xdoc.Element("library").Add(xele);
            }
            xdoc.Save(output);
        }
        public void groupByYear(string output)
        {
            XDocument xdoc = new XDocument(new XElement("library"));
            var years = from year in lib.Descendants("year")
                        select year.Value;
            years = years.Distinct();
            foreach (var year in years)
            {
                XElement xele = new XElement("year", new XElement("tracks"));
                xele.SetAttributeValue("value", year);
                var tracks = from track in lib.Descendants("track")
                             where track.Element("year").Value.Equals(year)
                             select track;
                foreach (var track in tracks)
                {
                    xele.Element("tracks").Add(track);
                }
                xdoc.Element("library").Add(xele);
            }
            xdoc.Save(output);
        }
        public XDocument findByKeyword(string keyword)
        {
            keyword = keyword.Trim().ToUpper();
            XDocument xdoc = new XDocument(new XElement("tracks"));
            var tracks = from track in lib.Descendants("track")
                         where track.Element("title").Value.Length >= keyword.Length && track.Element("title").Value.Substring(0, keyword.Length).ToUpper().Equals(keyword)
                         select track;
            var tracks1 = from track in lib.Descendants("track")
                          where track.Element("artist").Value.Length >= keyword.Length && track.Element("artist").Value.Substring(0, keyword.Length).ToUpper().Equals(keyword)
                          select track;
            var tracks2 = from track in lib.Descendants("track")
                          where track.Element("album").Value.Length >= keyword.Length && track.Element("album").Value.Substring(0, keyword.Length).ToUpper().Equals(keyword)
                          select track;
            var tracks3 = from track in lib.Descendants("track")
                          where track.Element("genre").Value.Length >= keyword.Length && track.Element("genre").Value.Substring(0, keyword.Length).ToUpper().Equals(keyword)
                          select track;
            tracks = tracks.Union(tracks1).Union(tracks2).Union(tracks3);
            foreach (var track in tracks)
            {
                xdoc.Element("tracks").Add(track);
            }
            return xdoc;
        }
    }
}
